package com.gun3y.ts.helper;

import com.gun3y.entity.media.PropertyType;

public class SparqlHelper {
    public static final String PREFIX_SW = "PREFIX sw: <http://www.gun3y.com/ontology/movie#>";
    public static final String PREFIX_RDF = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>";
    public static final String PREFIX_WRP = "PREFIX wrp: <http://www.gun3y.com/ontology/wrapper#>";

    public static String askByName(String name) {
	StringBuilder builder = new StringBuilder();
	builder.append(PREFIX_SW).append("\n");
	builder.append(PREFIX_RDF).append("\n");
	builder.append(PREFIX_WRP).append("\n");
	builder.append("SELECT ?node WHERE {").append("\n");
	builder.append("\t?node rdf:type sw:Movie.").append("\n");
	builder.append("\t?node sw:name \"" + name).append("\"\n");
	builder.append("}\n");
	return builder.toString();
    }

    public static String askByValue(String movieName, PropertyType propertyType, String text) {
	StringBuilder builder = new StringBuilder();
	builder.append(PREFIX_SW).append("\n");
	builder.append(PREFIX_RDF).append("\n");
	builder.append(PREFIX_WRP).append("\n");
	builder.append("SELECT ?node WHERE {").append("\n");
	builder.append("\t?movie rdf:type sw:Movie.").append("\n");
	builder.append("\t?movie sw:name \"" + movieName).append("\".\n");
	builder.append("\t?movie sw:" + propertyType + " ?node").append(".\n");
	builder.append("\t?node sw:text \"").append(text).append("\".\n");
	builder.append("}\n");
	return builder.toString();
    }

    public static void main(String[] args) {
	System.out.println("asidaşsldad dakdasdlk\nsd".replaceAll(" ", "~").replaceAll("\\s+", "").replaceAll("~", " "));
    }

    public static String askByDomain(String movieName, PropertyType propertyType, String text, String from) {
	StringBuilder builder = new StringBuilder();
	builder.append(PREFIX_SW).append("\n");
	builder.append(PREFIX_RDF).append("\n");
	builder.append(PREFIX_WRP).append("\n");
	builder.append("SELECT ?node WHERE {").append("\n");
	builder.append("\t?movie rdf:type sw:Movie.").append("\n");
	builder.append("\t?movie sw:name \"" + movieName).append("\".\n");
	builder.append("\t?movie sw:" + propertyType + " ?node").append(".\n");
	builder.append("\t?node sw:text \"").append(text).append("\".\n");
	builder.append("\t?node sw:from \"").append(from).append("\".\n");
	builder.append("}\n");
	return builder.toString();
    }

    public static String selectMovieNames() {
	StringBuilder builder = new StringBuilder();
	builder.append(PREFIX_SW).append("\n");
	builder.append(PREFIX_RDF).append("\n");
	builder.append(PREFIX_WRP).append("\n");
	builder.append("SELECT ?name {").append("\n");
	builder.append("\t?movie rdf:type sw:Movie.").append("\n");
	builder.append("\t?movie sw:name ?name").append("\n");
	builder.append("}\n");
	return builder.toString();
    }

    public static String selectMovieByName(String name) {
	StringBuilder builder = new StringBuilder();
	builder.append(PREFIX_SW).append("\n");
	builder.append(PREFIX_RDF).append("\n");
	builder.append(PREFIX_WRP).append("\n");
	builder.append("SELECT ?predicate ?dataNodeText ?dataNodeFrom").append("\n");
	builder.append("WHERE {").append("\n");

	PropertyType[] values = PropertyType.values();
	for (PropertyType propertyType : values) {
	    if (propertyType != PropertyType.name)
		builder.append(createUnion(name, propertyType)).append("\n");
	}

	return builder.append("}").toString().replaceFirst("UNION", "");
    }

    private static String createUnion(String name, PropertyType propertyType) {
	StringBuilder builder = new StringBuilder();
	builder.append("\tUNION {\n ");
	builder.append("\t\t?movie rdf:type sw:Movie.").append("\n");
	builder.append("\t\t?movie sw:name").append(" \"" + name + "\".\n");
	builder.append("\t\t?movie sw:").append(propertyType).append(" ?dataNode.\n");
	builder.append("\t\t?movie ").append("?predicate").append(" ?dataNode.\n");
	builder.append("\t\t?dataNode").append(" sw:text").append(" ?dataNodeText.\n");
	builder.append("\t\t?dataNode").append(" sw:from").append(" ?dataNodeFrom.\n");

	builder.append("\t}");
	return builder.toString();
    }
}
